package router

import (
	"dhuviewer/dto"
	jwt "dhuviewer/jwt"
	"dhuviewer/services"
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func Social(rg *gin.RouterGroup) {
	routerGroup := rg.Group("/")
	routerGroup.GET("/info/:uid", func(c *gin.Context) {
		userId := c.Param("uid")
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		fmt.Println(payload)
		if userId == "" {
			userId = payload.Uid
		}
		err, socailData := services.GetSocail(uuid.MustParse(userId))
		if err != nil {
			HelpError(c)
			return
		}
		c.JSON(200, gin.H{
			"status":   20000,
			"uid":      socailData.Uid,
			"username": socailData.Username.String,
			"tail":     socailData.Tail.String,
			"honor":    socailData.Honor,
			"level":    socailData.Level.String,
			"tags":     socailData.Tags,
		})
	})

	routerGroup.GET("/infoWithSub/:uid", func(c *gin.Context) {
		userId := c.Param("uid")
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		// fmt.Println(payload)
		if userId == "" {
			HelpError(c)
		}
		err, socailData, subed := services.GetSocailWithSub(uuid.MustParse(payload.Uid), uuid.MustParse(userId))
		if err != nil {
			HelpError(c)
			return
		}
		c.JSON(200, gin.H{
			"status":   20000,
			"uid":      socailData.Uid,
			"username": socailData.Username.String,
			"tail":     socailData.Tail.String,
			"honor":    socailData.Honor,
			"level":    socailData.Level.String,
			"tags":     socailData.Tags,
			"subed":    subed,
		})
	})

	routerGroup.GET("/info/", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		// fmt.Println(payload)
		userId := payload.Uid
		err, socailData := services.GetSocail(uuid.MustParse(userId))
		if err != nil {
			HelpError(c)
			return
		}
		c.JSON(200, gin.H{
			"status":   20000,
			"uid":      socailData.Uid,
			"username": socailData.Username.String,
			"tail":     socailData.Tail.String,
			"honor":    socailData.Honor,
			"level":    socailData.Level.String,
			"tags":     socailData.Tags,
		})
	})

	routerGroup.POST("/updateLevel", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		if payload.Level == "banned" || payload.Level == "user" {
			fmt.Println("in")
			HelpError(c)
			return
		}
		json := make(map[string]interface{}) //注意该结构接受的内容
		c.BindJSON(&json)
		err = services.UpdateSocailLevel(uuid.MustParse(json["uid"].(string)), json["level"].(string))
		if err != nil {
			fmt.Println(err)
			HelpError(c)
			return
		} else {
			if payload.Uid == json["uid"].(string) {
				accessToken, _ := jwt.SignJwt(payload.Username, json["level"].(string), payload.Uid)
				c.JSON(200, gin.H{
					"status":      20000,
					"accessToken": accessToken,
				})
				return
			} else {
				c.JSON(200, gin.H{
					"status": 20000,
				})
				return
			}
		}
	})

	routerGroup.POST("/updateHonor", func(c *gin.Context) {
		_, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		json := make(map[string]interface{}) //注意该结构接受的内容
		c.BindJSON(&json)
		err = services.UpdateSocailHonor(uuid.MustParse(json["uid"].(string)), int32(json["honor"].(float64)))
		if err != nil {
			fmt.Println(err)
			HelpError(c)
			return
		} else {
			HelpOK(c)
		}
	})

	routerGroup.POST("/updateInfo", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		fmt.Println(payload)
		socailInfo := new(dto.Socail) //注意该结构接受的内容
		c.ShouldBind(&socailInfo)
		// fmt.Println(payload.Uid)
		// fmt.Println(socailInfo)
		err = services.UpdateUserSocail(uuid.MustParse(payload.Uid), socailInfo)
		// fmt.Println(err)
		HelpErrorWithCode(c, err)
	})

	routerGroup.GET("/allSocail", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		if payload.Level == "user" || payload.Level == "banned" {
			HelpError(c)
			return
		}
		res, err := services.GetAllSocailData()
		HelpErrorWithReturn(c, err, res)
	})
}
